import { getToken, setToken, removeToken } from '@/utils/auth'
const user = {
  state: () => ({
    userInfo: getToken()
  }),
  mutations: {
    setUserInfo(state, result) {
      state.userInfo = result
      setToken(result)
    },
    removeUserInfo(state) {
      state.userInfo = {}
      removeToken()
    }
  },
  actions: {
    getUserInfo(context, data) {
      context.commit('setUserInfo', data)
    },
    logout(context) {
      context.commit('removeUserInfo')
    }
  },
  getters: {
    getUser: state => state.userInfo,
    getUserAvatar(state) {
      return state.userInfo.userAvatar
    },
    getUserName(state) {
      return state.userInfo.username

    },
    getUserEmail(state) {
      return state.userInfo.email
    },
    getUserId(state) {
      return state.userInfo.userId
    },
    getUserRole(state) {
      return state.userInfo.isAdmin == '1'
    },
    getToken(state) {
      return state.userInfo && state.userInfo.tokenMap &&  state.userInfo.tokenMap.token
    },
  },
  namespaced: true,
}
export default user